package vcs.media;

import java.util.ArrayList;
import java.util.HashMap;

import vcs.media.player.SongManager;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Artist extends ListActivity{

	TextView artist;
	ListView lv;
	public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
	int songIndex;
	String type;
	SongManager song;
	ArrayList<String> arr;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Window w = getWindow();
		
		//setContentView(R.layout.artist);
		
		//artist = (TextView)findViewById(R.id.artist);
		//lv = (ListView)findViewById(R.id.listArt);
		
		Bundle bundle = getIntent().getExtras();
		type = bundle.getString("mediatype");
		//type = bundle.getString("author");
		
		if(type.equals("artist")){
			setTitle("Artist");
			song = new SongManager();
			arr = new ArrayList<String>();
			arr = song.getContainMedia(SongManager.ARTIST);
		}
		
		if(type.equals("author")){
			setTitle("Author");
			song = new SongManager();
			arr = new ArrayList<String>();
			arr = song.getContainMedia(SongManager.AUTHOR);
		}
		
		if(type.equals("genre")){
			setTitle("Genre");
			song = new SongManager();
			arr = new ArrayList<String>();
			arr = song.getContainMedia(SongManager.GENRE);
		}
		
//		song = new SongManager();
//		arr = new ArrayList<String>();
//		arr = song.getContainMedia(SongManager.ARTIST);
		setListAdapter(new ArrayAdapter<String>(this,
		         R.layout.playlist_item,R.id.songTitle, arr));
		ListView lvs = getListView();
		lvs.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent;
				
				if(type.equals("artist")){
					intent = new Intent(getApplicationContext(), DetailArtist.class);
					intent.putExtra("type", "artist");
					intent.putExtra("name", parent.getItemAtPosition(position).toString());
					startActivityForResult(intent, 200);
				}
				
				if(type.equals("author")){
					intent = new Intent(getApplicationContext(), DetailArtist.class);
					intent.putExtra("type", "author");
					intent.putExtra("name", parent.getItemAtPosition(position).toString());
					startActivityForResult(intent, 300);
				}
				
				if(type.equals("genre")){
					intent = new Intent(getApplicationContext(), DetailArtist.class);
					intent.putExtra("type", "genre");
					intent.putExtra("name", parent.getItemAtPosition(position).toString());
					startActivityForResult(intent, 400);
				}
				
			}
		});
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		
		
		if(resultCode == 200){
			
			Bundle bundleArt = data.getExtras();
			//String intentArt = bundleArt.getString("ArtistIntent");
			int index = bundleArt.getInt("songIndexType");
			//songsList = new ArrayList<HashMap<String, String>>();
			songsList = (ArrayList<HashMap<String,String>>)bundleArt.getSerializable("mediatype");
			
			Bundle bundle = new Bundle();
			bundle.putSerializable("mediatype", songsList);
			
			Intent in = new Intent(getApplicationContext(),
					MainRun.class);
			in.putExtra("songIndexArt", index);
			in.putExtras(bundle);
			setResult(200, in);
			
			finish();
		}
		
		if(resultCode == 300){
			Bundle bundleArt = data.getExtras();
			//String intentArt = bundleArt.getString("ArtistIntent");
			int index = bundleArt.getInt("songIndexType");
			//songsList = new ArrayList<HashMap<String, String>>();
			songsList = (ArrayList<HashMap<String,String>>)bundleArt.getSerializable("mediatype");
			
			Bundle bundle = new Bundle();
			bundle.putSerializable("mediatype", songsList);
			// Starting new intent
			Intent in = new Intent(getApplicationContext(),
					MainRun.class);
			// Sending songIndex to PlayerActivity
			//in.putExtra("ArtistIntent", "ThisArt");
			in.putExtra("songIndexArt", index);
			in.putExtras(bundle);
			//startActivity(in);
			setResult(300, in);
			// Closing PlayListView
			finish();
		}
		
		if(resultCode == 400){
			Bundle bundleArt = data.getExtras();
			//String intentArt = bundleArt.getString("ArtistIntent");
			int index = bundleArt.getInt("songIndexType");
			//songsList = new ArrayList<HashMap<String, String>>();
			songsList = (ArrayList<HashMap<String,String>>)bundleArt.getSerializable("mediatype");
			
			Bundle bundle = new Bundle();
			bundle.putSerializable("mediatype", songsList);
			// Starting new intent
			Intent in = new Intent(getApplicationContext(),
					MainRun.class);
			// Sending songIndex to PlayerActivity
			//in.putExtra("ArtistIntent", "ThisArt");
			in.putExtra("songIndexArt", index);
			in.putExtras(bundle);
//			startActivity(in);
			setResult(400, in);
			// Closing PlayListView
			finish();
		}
	}
}

